/*
 * @Author: eycode
 * @Date: 2021-05-21 11:17:52
 * @LastEditTime: 2021-05-21 11:25:40
 * @LastEditors: eycode
 * @Description: 人物
 * @Other: 
 */

import Rocker from "./Rocker";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    /**摇杆 */
    @property(Rocker)
    Rocker: Rocker = null;

    /**移动的速度 */
    private _speed: number = 200;

    update(dt: number) {
        if (this.Rocker.dir.mag() < 0.5) {
            return;
        }

        var vx = this.Rocker.dir.x * this._speed;
        var vy = this.Rocker.dir.y * this._speed;

        var sx = vx * dt;
        var sy = vy * dt;

        //移动
        this.node.x += sx;
        this.node.y += sy;

        //方向计算
        var r = Math.atan2(this.Rocker.dir.y, this.Rocker.dir.x);
        var degree = r * 180 / (Math.PI);
        degree = 360 - degree + 90;
        this.node.rotation = degree;
    }
}
